package cn.wolfcode.web.controller;

import cn.wolfcode.domain.City;
import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Province;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;
@Controller
public class JsonController {

    @RequestMapping("/city")
    @ResponseBody
    public List<City> city(Long pid){
        return City.getCityByProvinceId(pid);
    }

    @RequestMapping("/province")
    @ResponseBody
    public List<Province> province(){
        return  Province.getAllProvince();
    }


    @RequestMapping("/loginJson")
    @ResponseBody
    public JsonResult loginJson(String username,String password){
        JsonResult jsonResult = new JsonResult();

        if("lony".equals(username) && "123".equals(password)){
            jsonResult.setSuccess(true);
            jsonResult.setMsg("成功");
        }else {
            jsonResult.setSuccess(false);
            jsonResult.setMsg("失败");
        }
        return jsonResult;
    }


    @RequestMapping("/existName")
    @ResponseBody
    public JsonResult existName(String username){
        JsonResult jsonResult = new JsonResult();

        if("lony".equals(username)){
            jsonResult.setSuccess(false);
            jsonResult.setMsg("名字雷同");
        }else {
            jsonResult.setSuccess(true);
            jsonResult.setMsg("你很秀");
        }
        return jsonResult;
    }




    @Autowired
    private IDepartmentService departmentService;
    @RequestMapping("/gettime")
    @ResponseBody
    public JsonResult getTime(){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg(new Date().toLocaleString());
        jsonResult.setSuccess(true);
        return jsonResult;
    }
    @RequestMapping("/depts")
    @ResponseBody
    public List<Department> depts(){
        return departmentService.listAll();
    }
}
